package com.dmtt.web.common;

import org.springframework.stereotype.Component;

@Component
public class CheckCode {
    private String makeChecksum(String[] hexdata) {
        if (hexdata == null || hexdata.equals("")) {
            return "00";
        }
        int total = 0;
        for (int i = 1;i<hexdata.length;i++) {
            total = total + Integer.parseInt(hexdata[i], 16);
        }
        String str = Integer.toHexString(total);
        String code = hexString(str);
        return code;
    }

    private String hexString(String total){
        int a = Integer.parseInt("0100",16) - Integer.parseInt(total.substring(total.length()-2),16);
        String str = Integer.toHexString(a);
        if (str.length() == 1){
            str =  "0" + str;
        } else if (str.length() >= 3){
            str = str.substring(str.length()-2);
        }
        return str;
    }

    public String checkCode(String code){
        String[] arr = code.split(" ");
        String sum = makeChecksum(arr);
        String codes = code + " " + sum.toUpperCase() + " AA";
        return codes;
    }


}
